package tongxin1;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class test3Reen {
    public static void main(String[] args) {
        Printer3 printer3 = new Printer3();
        new Thread(() -> {
            while (true) {
                try {
                    printer3.print1();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
        new Thread(() -> {
            while (true) {
                try {
                    printer3.print2();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        }).start();
        new Thread(() -> {
            while (true) {
                try {
                    printer3.print3();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }
}

class Printer3 {
    ReentrantLock rtl = new ReentrantLock();
    Condition c1 = rtl.newCondition(); // 线程1的状态
    Condition c2 = rtl.newCondition(); // 线程2的状态
    Condition c3 = rtl.newCondition(); // 线程3的状态
    int flag = 1;

    public void print1() throws InterruptedException {
        rtl.lock();
        while (flag != 1) {
            c1.await();
        }
        System.out.println("a");
        flag = 2;
        c2.signal();
        rtl.unlock();
    }

    public void print2() throws InterruptedException {
        rtl.lock();
        while (flag != 2) {
            c2.await();
        }
        ;
        System.out.println("b-------------");
        flag = 3;
        c3.signal();
        rtl.unlock();

    }

    public void print3() throws InterruptedException {
        rtl.lock();
        while (flag != 3) {
            c3.await();
        }
        ;
        System.out.println("c**********");
        flag = 1;
        c1.signal();
        rtl.unlock();
    }
}
























